﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Projekt
{ 
    public partial class newShop : Form
    {
        private DataTable shopNames;
        private string nameInputErrorMsg = "Blank input box not accepted";
        private string phoneNumberInputErrorMsg = "";
        private ToolTip tt = new ToolTip();

        public newShop()
        {
            InitializeComponent();
        }

        private void newShop_Load(object sender, EventArgs e)
        {
            try
            {
                Shop ceg = new Shop();
                shopNames = ceg.getShopList();
            }
            catch (Exception es) { MessageBox.Show(es.ToString()); }
        }
    /*  ---------------------------------------------
     *  |           Input validating:               |   
     *  |shopName:                                  |
     *  |   not exist in database                   |
     *  |   1 or more character                     |
     *  |shopPhoneNumber:                           |
     *  |   10 or more digit                        |
     *  |   only numeric character beetwen 0 and 9  |
     *  ---------------------------------------------
     *  */
        private void shopPhoneNumber_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                phoneNumberInputErrorMsg = "";
                if (shopTelNumber.Text.ToString().Length > 0)
                {
                    Int64 numberEntered = Int64.Parse(shopTelNumber.Text);
                    if (shopTelNumber.Text.ToString().Length < 10)
                    {
                        phoneNumberInputErrorMsg = phoneNumberInputErrorMsg + "Phone number is 10 or more digit\n";
                        newShopSave.Enabled = false;
                        badInputWarning.Visible = true;
                    }
                }
            }
            catch (FormatException es)
            {
                newShopSave.Enabled = false;
                badInputWarning.Visible = true;
                phoneNumberInputErrorMsg = phoneNumberInputErrorMsg + es.ToString();
            }
        }
        private void shopPhoneNumber_Validated(object sender, EventArgs e)
        {
            if (phoneNumberInputErrorMsg == "")
            {
                if (phoneNumberInputErrorMsg=="" && nameInputErrorMsg=="") newShopSave.Enabled = true;
                badInputWarning.Visible = false;
            }
        }     
        private void shopName_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                if (shopName.Text == "")
                {
                    nameInputErrorMsg = "Blank input box not accepted";
                    newShopSave.Enabled = false;
                    badInputShopName.Visible = true;
                    return;
                }
                else
                {
                    nameInputErrorMsg = "";
                    foreach (DataRow rows in shopNames.Rows)
                    {
                        if (rows["UNev"].ToString() == shopName.Text.ToString())
                        {
                            nameInputErrorMsg = nameInputErrorMsg + "This shop already exists";
                            newShopSave.Enabled = false;
                            badInputShopName.Visible = true;
                            break;
                        }
                    }
                }
            }
            catch (Exception es)
            {
                MessageBox.Show(es.ToString());
            }
        }
        private void shopName_Validated(object sender, EventArgs e)
        {
            if (nameInputErrorMsg == "")
            {
                if (phoneNumberInputErrorMsg == "" && nameInputErrorMsg == "") newShopSave.Enabled = true;
                badInputShopName.Visible = false;
            }
        }

        private void newShopSave_Click(object sender, EventArgs e)
        {
            try
            {
                Shop uzlet = new Shop();
                String error = "";
                if (!uzlet.add(shopName.Text.ToString(), shopTelNumber.Text.ToString(), shopAddress.Text.ToString(), error)) MessageBox.Show("Error");
                else this.Close();
            }
            catch (Exception es) { MessageBox.Show(es.ToString()); }
        }

        private void shopTelNumber_TextChanged(object sender, EventArgs e)
        {
            try
            {
                this.Validate();
            }
            catch (Exception es)
            {
                MessageBox.Show(es.ToString());
            }
        }

        private void shopName_TextChanged(object sender, EventArgs e)
        {
        try
            {
                this.Validate();
            }
            catch (Exception es)
            {
                MessageBox.Show(es.ToString());
            }
        }

        private void nameInputWarning_MouseOver(object sender, EventArgs e)
        {
            try
            {
                
                String ttString = "";
                if (nameInputErrorMsg != "") ttString = ttString + "Name: " + nameInputErrorMsg + "\n";
                if (phoneNumberInputErrorMsg != "") ttString = ttString + "Phone number: " + phoneNumberInputErrorMsg + "\n";
                tt.Show(ttString, (PictureBox)sender);
            }
            catch (Exception es) { MessageBox.Show(es.ToString()); }
        }

        private void tooltpit_reset(object sender, EventArgs e)
        {
           
            tt.RemoveAll();
        }

    }
}
